package com.qk.management.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;
import com.qk.entity.User;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Course> getCoursesBySubject(Integer subject) {
        return courseMapper.selectBySubject(subject);
    }


    @Override
    public List<Course> getCourses() {
        return courseMapper.selectAll();
    }


    @Override
    public Integer updateCourse(Course course) {
        if (course.getId() == null)return 1;
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
        return 0;
    }




    @Override
    public Course getCourseById(Integer id) {
        return courseMapper.select(id);
    }



    @Override
    public void deleteCourseById(Integer ids) {
        courseMapper.delete(ids);
    }


    @Override
    public void addCourse(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }




    @Override
    public PageResult<Course> page(CourseDTO dto) {

        // 开始分页
        PageHelper.startPage(dto.getPage(), dto.getPageSize());

        // 执行查询
        List<Course> courses = courseMapper.page(dto.getName(),dto.getSubject(),dto.getTarget());

        // 获取分页信息
        Page<Course> pageInfo = (Page<Course>) courses;

        // 封装成 PageResult 对象
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());

    }


}
